package com.ziyougou.product.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.BrandDao;
import com.ziyougou.product.entity.BrandEntity;
import com.ziyougou.product.service.BrandService;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
      /*  IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );*/
        //1.模糊查条件
        LambdaQueryWrapper<BrandEntity> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(params.get("key"))){
            wrapper.like(BrandEntity::getName,params.get("key"));
        }
        //2.分页参数
        Long pageIndex= Long.parseLong((String)params.get("page"));
        Long pageSize=Long.parseLong((String)params.get("limit"));
        Page<BrandEntity> p1 = new Page<>(pageIndex,pageSize);
        //3.查询
        Page<BrandEntity> page = baseMapper.selectPage(p1, wrapper);
        return new PageUtils(page);
    }

    @Override
    public void updateStatusById(BrandEntity brand) {
       // baseMapper.updateById(brand);
        baseMapper.updateStatusById(brand);
    }

    @Value("${aliyun.endpoint}")
    private String endPoint;
    @Value("${aliyun.keyid}")
    private String accessKey;
    @Value("${aliyun.keysecret}")
    private String secreKey;
    @Value("${aliyun.bucketname}")
    private String bucketName;
    @Override
    public String fileUpload(MultipartFile file) {
            try {
                // 创建OSSClient实例。
                OSS ossClient = new OSSClientBuilder().build(endPoint, accessKey, secreKey);
                // 上传文件流。
                InputStream inputStream = file.getInputStream();
                String fileName = file.getOriginalFilename();
                //生成随机唯一值，使用uuid，添加到文件名称里面
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                fileName = uuid+fileName;
                //按照当前日期，创建文件夹，上传到创建文件夹里面
                //  2024/02/02/01.jpg
                String timeUrl = new DateTime().toString("yyyy/MM/dd");
                fileName = timeUrl+"/"+fileName;
                //调用方法实现上传
                PutObjectResult result=ossClient.putObject(bucketName, fileName, inputStream);
                // 关闭OSSClient。
                ossClient.shutdown();
             /*   System.out.println(result.getResponse().getStatusCode());
                System.out.println(result.getResponse().getErrorResponseAsString());
                System.out.println(result.getResponse().getUri());*/
                //上传之后文件路径
                // https://spyx-spyx.oss-cn-beijing.aliyuncs.com/01.jpg
                String url = "https://"+bucketName+"."+endPoint+"/"+fileName;
                //返回
                return url;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
    }
}